package cn.kduck.code.entity.query;

import cn.kduck.core.dao.definition.BeanDefDepository;
import cn.kduck.core.dao.definition.BeanEntityDef;
import cn.kduck.core.dao.query.QueryCreator;
import cn.kduck.core.dao.query.QuerySupport;
import cn.kduck.core.dao.sqlbuilder.ConditionBuilder.ConditionType;
import cn.kduck.core.dao.sqlbuilder.SelectBuilder;
import org.springframework.stereotype.Component;

import java.util.Map;

import static cn.kduck.code.entity.service.EntityService.CODE_ENTITY_FIELD;

/**
 * 根据实体ID和实体字段编码，检测实体字段是否存在，用于检测实体字段编码是否重复
 */
@Component
public class EntityFieldExistQuery implements QueryCreator {

    @Override
    public QuerySupport createQuery(Map<String, Object> paramMap, BeanDefDepository depository) {
        BeanEntityDef entityDef = depository.getEntityDef(CODE_ENTITY_FIELD);
        SelectBuilder selectBuilder = new SelectBuilder(entityDef,paramMap);
        selectBuilder.where()
                .and("ENTITY_ID", ConditionType.EQUALS,"entityId",true)
                .and("FIELD_CODE", ConditionType.EQUALS,"fieldCode",true);
        return selectBuilder.build();
    }
}
